﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace WGCSFramework.images
{
    public class WgFileIoImage
    {
        public static void saveJpeg(Image image, int width, int height, string folder, string file, int quality)
        {
            ImageCodecInfo codec = WgImages.GetEncoderInfo("image/jpeg");
            WgFileIoImage.saveFile(image, width, height, folder, file, quality, codec);
        }

        public static void savePng(Image image, int width, int height, string folder, string file, int quality)
        {
            ImageCodecInfo codec = WgImages.GetEncoderInfo("image/png");
            WgFileIoImage.saveFile(image, width, height, folder, file, quality, codec);
        }

        public static void saveGif(Image image, int width, int height, string folder, string file, int quality)
        {
            ImageCodecInfo codec = WgImages.GetEncoderInfo("image/gif");
            WgFileIoImage.saveFile(image, width, height, folder, file, quality, codec);
        }

        public static void saveTiff(Image image, int width, int height, string folder, string file, int quality)
        {
            ImageCodecInfo codec = WgImages.GetEncoderInfo("image/tiff");
            WgFileIoImage.saveFile(image, width, height, folder, file, quality, codec);
        }

        public static void saveBmp(Image image, int width, int height, string folder, string file, int quality)
        {
            ImageCodecInfo codec = WgImages.GetEncoderInfo("image/bmp");
            WgFileIoImage.saveFile(image, width, height, folder, file, quality, codec);
        }

        private static void saveFile(Image image, int width, int height, string folder, string file, int quality, ImageCodecInfo codec)
        {
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
            if (width == image.Width && height == image.Height) image.Save(folder + file, ImageFormat.Jpeg);
            else
            {
                Image rim = WgResizer.ResizeBitmap(image, width, height);
                rim.Save(folder + file, codec, encoderParams);
            }
        }

    }
}
